package cs5226.tags;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import cs5226.dao.ConfigurationDAO;
import cs5226.model.SingleValueStatistics;
import cs5226.model.ThresholdBean;
import cs5226.util.StringUtil;

public class ThresholdUtil {
	public static final String SHARED_POOL_FREE = "Shared Pool Free %";
	public static final String LIBRARY_CACHE_HIT_RATIO = "Library Cache Hit Ratio";
	public static final String DICTIONARY_CACHE_HIT_RATIO = "Dictionary Cache Hit Ratio";
	public static final String BUFFER_CACHE_HIT_RATIO = "Buffer Cache Hit Ratio";
	public static final String REDO_BUFFER_ALLOCATION_HIT = "Redo Buffer Allocation Hit";
	public static final String REDO_LOG_SWITCH_INCOMPLETE_CHECKPOINT_WAIT_TIME = "Redo Log Switch (Incomplete Checkpoint) Wait Time";
	public static final String MEMORY_SORT_PCT = "Memory Sort Percentage";
	public static final String ROLLBACK_PCT = "Rollback Percentage";
	public static final String ROLLBACK_PER_SECOND = "Rollback Per Second";
	
	public static ThresholdBean getThresholdValues (String statistics, HttpServletRequest request) throws Exception {
		String statisticsThreshold = statistics + "-Threshold";
		
		ThresholdBean theshold = (ThresholdBean) request.getAttribute(statisticsThreshold);
		if (theshold != null) return theshold;
		
		ArrayList<ThresholdBean> allTHs = ConfigurationDAO.getThresholds();
		for (ThresholdBean t : allTHs) {
			if (t.getParamName().equals(statistics)) {
				request.setAttribute(statisticsThreshold, t);
				return t;
			}
		}
		return null;
	}
	
	public static String getHtmlHealthStatus (String statistics, HttpServletRequest request, SingleValueStatistics stat) {
		try {
			ThresholdBean threshold = ThresholdUtil.getThresholdValues(statistics, request);
			if (threshold == null) {
				return ("[error: Threshold '" + statistics + "' not yet defined]");
			}
			return getHtmlHealthStatus(statistics, threshold.assess(stat), threshold);
			//return assessHTML (threshold, stat);
		} catch (Exception e) {
			e.printStackTrace();
			return ("[error: " + e.getMessage() + "]");			
		}	
	}
	
	public static String getHtmlHealthStatus (String statistics, int status, ThresholdBean t) {
		if (status == ThresholdBean.CRITICAL) return "<a href='#'  style='color:red;' onclick=\"showAdvise('" + statistics + "')\" >CRITICAL" + "</a>";
		else if (status == ThresholdBean.UNHEALTHY) return "<span style='color:yellow; background-color:#B0ACAE'>WARNING</span>";
		else return "<span style='color:green;'>NORMAL</span>";
	}
	
	/*
	public static String assessHTML (ThresholdBean t, SingleValueStatistics stat) {
		double value = stat.getValue();
		double critical = t.getCritical();
		double unhealthy = t.getUnhealthy();
		
		if (critical >= unhealthy) {
			if (value >= critical) return "<span style='color:red;'>CRITICAL</span>:" + 
					StringUtil.formatDouble(unhealthy) + " &lt; " + StringUtil.formatDouble(critical) + " &lt; " + 
					StringUtil.formatDouble(value);
			else if (value >= unhealthy) return "<span style='color:yellow; background-color:#B0ACAE'>WARNING</span>" +
					StringUtil.formatDouble(unhealthy) + " &lt; " + StringUtil.formatDouble(value) + " &lt; " + 
					StringUtil.formatDouble(critical);
			else return "<span style='color:green;'>NORMAL</span>" + 
					StringUtil.formatDouble(value) + " &lt; " + StringUtil.formatDouble(unhealthy) + " &lt; " + 
					StringUtil.formatDouble(critical);
		}
		else {
			if (value <= critical) return "<span style='color:red;'>CRITICAL</span>:" + 
					StringUtil.formatDouble(value) + " &lt; " + StringUtil.formatDouble(critical) + " &lt; " + 
					StringUtil.formatDouble(unhealthy);
			else if (value <= unhealthy) return "<span style='color:yellow; background-color:#B0ACAE'>WARNING</span>" +
					StringUtil.formatDouble(critical) + " &lt; " + StringUtil.formatDouble(value) + " &lt; " + 
					StringUtil.formatDouble(unhealthy);
			else return "<span style='color:green;'>NORMAL</span>" + 
					StringUtil.formatDouble(critical) + " &lt; " + StringUtil.formatDouble(unhealthy) + " &lt; " + 
					StringUtil.formatDouble(value);
		}
	}
	*/
}
